#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "InterfaceUser.h"
#include "UDPFarfetchClient.c"

#define MILI_PER_SECOND 1000


/**
 * This program tests the time requests
 * 
 * @author Marcelo A. Nagatomo - RA 062689
 */

int main(int argc, char** argv)  {
	if(argc != 3){
		printf("*** TestError!! ***");
		printf("Use: './tcpClientTest <opt> <numberOfTests>'");
		return -1;
	}
	
	// get option to test
	Request req;
	req.option = atoi(argv[1]);
	
	// get number of tests
	int numberOfTests = atoi(argv[2]);
	
	// file data 
	FILE* fp;
	if((fp = fopen("tests.csv", "w+")) == NULL ){
		perror("An error occurred while create/open a file database.");
		exit(-1);
	}
	
	int i, count=0;
	fprintf(fp, "opt, time\n");
 	for(i=0; i<numberOfTests; i++){
 		req.option = rand() % 6 + 1;
 		req.value = rand() % 13 + 1;
		req.rate = rand() % 10 + 1;
 		
 		// test time
 		struct timeval start_time;
 		struct timeval stop_time;
 
 		gettimeofday( &start_time, NULL );
		//printf("Rand: %d [%d]\n", req.option, req.value);
 		executeRequest(&req, &count);
 		gettimeofday( &stop_time, NULL );
 		
 		float value = (float)(stop_time.tv_sec - start_time.tv_sec);//t2;// - t1;
 		value += (stop_time.tv_usec - start_time.tv_usec)/(float)MILI_PER_SECOND;
 						
 		// print timer
 		fprintf(fp, "%d, %.3f\n", req.option, value);
 		printf("%d) %d, %.3f\n", i, req.option, value);
 	}
	
	// printf("opt: [%d]\nnumberOfSize: [%d]\n", optReq, numberOfTests);
	// printf("Rand: [%d]\n", req.value);
	
	
	return 0;
}
